from django.db import transaction
from django.shortcuts import render
from rest_framework import permissions, views
from utils.custom_modelviewset import CustomModelViewSet
from utils import response_ok, response_error
from utils.exceptions import CustomError
from .models import Option, Area
from .serializers import OptionSerializer, AreaSerializer
from .filters import OptionFilter


class DictView(views.APIView):
    permission_classes = [permissions.IsAdminUser, ]

    def get(self, rquest):
        return response_ok({
            'option_type': Option.TYPE_JSON
        })


class OptionViewSet(CustomModelViewSet):
    permission_classes = [permissions.IsAdminUser, ]
    queryset = Option.objects.filter()
    serializer_class = OptionSerializer

    def filter_queryset(self, queryset):
        f = OptionFilter(self.request.GET, queryset=queryset)
        return f.qs


class AreaViewSet(CustomModelViewSet):
    queryset = Area.objects.filter()
    serializer_class = AreaSerializer

    # def create(self, request, *args, **kwargs):
    #     try:
    #         print(request.data)
    #     except CustomError as e:
    #         return response_error(e.get_error_msg())
    #     except Exception as e:
    #         return response_error(str(e))
    #     return response_ok()
